#pragma once
#include <assert.h>

class Date
{
public:
	int GetMonthDay(int year, int month)
	{
		assert(year > 1 && month >= 1 && month <= 12);
		int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			return 29;
		}
		return arr[month];
	}
	Date(int year = 2023, int month = 11, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}

	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}
	~Date()
	{
		_year = _month = _day = 0;
	}

	Date& operator+=(int day)
	{
		if (day < 0)
		{
			return *this -= (-day);
		}
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month += 1;
			if (_month > 12)
			{
				_year += 1;
				_month = 1;
			}
		}
		return *this;
	}
	Date operator+(int day) const
	{
		Date tmp(*this);
		tmp += day;
		return tmp;
	}
	Date& operator-=(int day)
	{
		if (day < 0)
		{
			return *this += (-day);
		}
		_day -= day;
		while (_day < 0)
		{
			_month -= 1;
			if (_month == 0)
			{
				_year -= 1;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	Date operator-(int day)const
	{
		Date tmp = *this;
		tmp -= day;
		return tmp;
	}

	bool operator==(const Date& d)const
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
	bool operator>(const Date& d)const
	{
		if (_year > d._year)
			return true;
		if (_year < d._year)
			return false;
		if (_month > d._month)
			return true;
		if (_month < d._month)
			return false;
		if (_day > d._day)
			return true;
		else
			return false;
	}

	bool operator>=(const Date& d)const
	{
		return *this == d || *this > d;
	}

	bool operator<(const Date& d)const
	{
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const
	{
		return !(*this > d);
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}

	Date& operator++()
	{
		return (*this) += 1;
	}
	Date operator++(int)
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}
	Date& operator--()
	{
		return *this -= 1;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}

	int operator-(const Date& d)const
	{
		Date min = d;
		Date max = *this;
		int flag = 1;
		if (*this < d)
		{
			flag = -1;
			min = *this;
			max = d;
		}
		int ret = 0;
		while (min != max)
		{
			ret++;
			++min;
		}
		return ret * flag;
	}
private:
	int _year;
	int _month;
	int _day;
};

void test()
{
	//Date d1;
	//Date d2(2020, 10, 1);
	//d1 = d2;
	//Date d3(d1);
	//d1 += 1000;
	//d2 = d1 + 100;
	//d3 -= 100000;
	//d1 = d2 - 100000;

	//d1 += -1000;
	//d1 -= -1000;

	Date d1(2023, 11, 2);
	//Date d2(2023, 11, 1);
	Date d2(1949, 10, 1);

	cout << (d2 - d1);
}

